# A.TNG

import cgi
import datetime
import wsgiref.handlers
import urllib
import os

from xnshitang import xndb

from google.appengine.ext import db
from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template

class Greeting(db.Model):
  author = db.UserProperty()
  content = db.StringProperty(multiline=True)
  date = db.DateTimeProperty(auto_now_add=True)


class MainPage(webapp.RequestHandler):
  def get(self):
    path = os.path.join(os.path.dirname(__file__), 'index.htm')
    self.response.out.write(template.render(path, {}))

class Guestbook(webapp.RequestHandler):
  def post(self):
    greeting = Greeting()

    if users.get_current_user():
      greeting.author = users.get_current_user()

    greeting.content = self.request.get('content')
    greeting.put()
    self.redirect('/xnshitang/')

class AddComments(webapp.RequestHandler):
    def get(self):
        path = os.path.join(os.path.dirname(__file__), 'addcomments.html')
        self.response.out.write(template.render(path, {}))
    def post(self):
        pass

class TestXNDataStore(webapp.RequestHandler):
    def get(self):
        self.response.headers['Content-Type'] = 'text/html'
        self.response.out.write(xndb.delete_comment(8))
        
class PostPage(webapp.RequestHandler):
    def get(self):
        path = os.path.join(os.path.dirname(__file__), 'post.htm')
        self.response.out.write(template.render(path, {}))

application = webapp.WSGIApplication([
                                      ('/xnshitang/', MainPage),
                                      ('/xnshitang/sign', Guestbook),
                                      ('/xnshitang/post', PostPage),
                                      ('/xnshitang/testxndatastore', TestXNDataStore),
                                      ], debug=True)


def main():
  wsgiref.handlers.CGIHandler().run(application)


if __name__ == '__main__':
  main()
    